package com.atguigu.tingshu.user.strategy;

import com.atguigu.tingshu.common.execption.GuiguException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Package: com.atguigu.tingshu.user.strategy
 *
 * @author xiongkunYang
 * @description: ItemType 策略工厂：封装可能存在的变化（具体策略实现类）
 * @since 2025/6/26 10:20
 */
@Component
@Slf4j
public class ItemTypeStrategyFactory {

    /**
     * 将IOC容器中ItemTypeStrategy接口下所有实现类对象注入到Map中
     * Map中Key：Bean对象ID
     * Map中Value：实现类对象
     * {"1001":购买类型为专辑策略类对象}
     * {"1002":购买类型为声音策略类对象}
     * {"1003":购买类型为会员策略类对象}
     */
    private final Map<String, ItemTypeStrategy> itemTypeStrategyMap;

    public ItemTypeStrategyFactory(Map<String, ItemTypeStrategy> itemTypeStrategyMap) {
        this.itemTypeStrategyMap = itemTypeStrategyMap;
    }

    /**
     * 根据购买项目类型获取具体策略实现类对象
     *
     * @param itemType 项目类型
     * @return 策略实现类对象
     */
    public ItemTypeStrategy getItemTypeStrategy(String itemType) {
        if (itemTypeStrategyMap.containsKey(itemType)) {
            return itemTypeStrategyMap.get(itemType);
        }
        log.error("该策略实现类不存在");
        throw new GuiguException(500, "该策略" + itemType + "实现类不存在");
    }
}